<template>
  <div class="map-component">
    <line-list />
    <baidu-map v-loading="false" style="height: 100%" :scroll-wheel-zoom="true" :center="center" :zoom="15" @click="mapClick">
      <bm-marker v-for="(item, index) in positions" :key="index" :position="item" :dragging="true" />
      <bm-polygon
        :path="polygonPath"
        stroke-color="blue"
        fill-color="red"
        :fill-opacity=".3"
        :stroke-opacity=".3"
        :stroke-weight="2"
        :editing="false"
        :clicking="true"
        @click="clickPolygon"
        @lineupdate="updatePolygonPath"
      />
    </baidu-map>
  </div>
</template>

<script>
import LineList from './LineList'
export default {
  components: {
    LineList
  },
  data() {
    return {
      center: {
        lng: 116.404, lat: 39.915
      },
      positions: [
        {
          lng: 116.404, lat: 39.915
        },
        {
          lng: 116.402, lat: 39.913
        }
      ],

      polygonPath: [
        {
          lng: 116.412732, lat: 39.911707
        },
        {
          lng: 116.39455, lat: 39.910932
        },
        {
          lng: 116.403461, lat: 39.921336
        }
      ]
    }
  },

  methods: {
    /**
     * 点击地图事件
     */
    mapClick(e) {
      console.log(e.point)
      // this.addPolygonPoint(e.point);
    },

    /**
     * 更新区域事件
     */
    updatePolygonPath(e) {
      this.polygonPath = e.target.getPath()
    },

    /**
     * 点击覆盖物事件
     */
    clickPolygon(e) {
      console.log('点击覆盖物', e)
    },

    /**
     * 添加新定位点
     */
    addPolygonPoint(pos) {
      this.polygonPath.push(pos)
    }
  }
}
</script>

<style lang="scss">
.map-component {
  width: 100%;
  height: 100%;
  position: relative;

  .map-component-line {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 100;
    width: 100%;
    padding: 5px;
    background-color: rgba(255, 255, 255, .5);

    .map-component-line-item {
      display: inline-block;
      background-color: red;
      border-radius: 3px;
      padding: 5px 8px;
      color: #fff;
      cursor: pointer;
      margin: 5px;
      font-size: 14px;
    }

    .map-component-line-item.active {
      background-color: blue;
    }
  }
}

.anchorBL {
  display: none;
}
</style>
